package com.hb.trade.api.routine.controller.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.project.section.dto.ClarifyDTO;
import fzzn.core.aspose.convert.AsposeConvertUtil;
import fzzn.core.spire.OfficeUtil;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.file.entity.HbFileInfo;
import org.fzzn.core.file.entity.UploadEntity;
import org.fzzn.core.file.service.PersistenceService;
import org.fzzn.core.model.constant.CommonConstant;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 发送短信的服务
 *
 * @author ljs
 * @date 2022-3-9
 */
@Slf4j
public class HTMLToPDFService {

    protected final PersistenceService persistenceService;
    public HTMLToPDFService(PersistenceService persistenceService){
        this.persistenceService = persistenceService;
    }

    public ResponseDto htmlToPDF(String content, String fileName) {
        if (StrUtil.isEmpty(content)) {
            return ResultUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //将澄清内容html片段转换为word文件流
        File wordFile = OfficeUtil.htmlStrToWordFile(content, null);
        File removeSpireDocWaterFile = OfficeUtil.removeSpireDocWater(wordFile);
        //再将word字节流转换为pdf字节流
        byte[] pdfContent = AsposeConvertUtil.wordToPDF(removeSpireDocWaterFile);
        //构建参数，上传至OSS文件服务器
        File pdfFile  = FileUtil.writeBytes(pdfContent, new File(CommonConstant.TEMP_PATH + "/" + fileName));
        UploadEntity uploadEntity = new UploadEntity(pdfFile);
        List<HbFileInfo> fileInfoList = new ArrayList<>();
        try {
            fileInfoList = persistenceService.uploadServer(uploadEntity);
        } catch (IOException e) {
            log.warn("澄清文件上传OSS服务器异常");
            e.printStackTrace();
        }
        return ResultUtil.success(MsgEnum.SUCCESS.msg(), fileInfoList);
    }
}
